package com.sit.library.service.impl;

import com.sit.library.entity.bean.Account;
import com.sit.library.entity.dao.AccountMapper;
import com.sit.library.service.AuthorizeService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AuthorizeServiceImpl implements AuthorizeService {

    @Resource
    private AccountMapper mapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Account account = this.findByUsername(username);
        if (account == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        return User
                .withUsername(account.getName())
                .password(account.getPassword())
                .roles(account.getRole())
                .build();
    }

    @Override
    public Account findByUsername(String username) {
        return mapper.findByUsername(username);
    }
}
